//
//  UserFeedBackController.m
//  takeout
//
//  Created by Mac on 12-1-7.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "UserFeedBackController.h"
#import "MoreManager.h"

@implementation UserFeedBackController
@synthesize textViews;

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //添加手势
    //后退
    UISwipeGestureRecognizer *recognizer; 
    recognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeFrom:)];
    [recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
    [[self view] addGestureRecognizer:recognizer];
    
    self.navigationController.navigationBarHidden = NO; 
    
    self.title = [recieveDictionary objectForKey:@"title"];
    //定义导航栏左侧按钮
    UIButton *leftButton = [[UIButton alloc] initWithFrame:CGRectMake(0,0,54,27)];
    NSString *filePath=[[NSBundle mainBundle] pathForResource:@"navLeftBtn" ofType:@"png"];
    [leftButton setBackgroundImage:[UIImage imageWithContentsOfFile:filePath] forState:UIControlStateNormal];
    [leftButton setBackgroundImage:[UIImage imageWithContentsOfFile:filePath] forState:UIControlStateHighlighted];
    [leftButton setTitle:@"返 回" forState:UIControlStateNormal];
    leftButton.titleLabel.font=[UIFont fontWithName:@"Helvetica-Bold" size:14]; 
    [leftButton addTarget:self action:@selector(backButtonPressd) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    //设置为左边按钮
    [[self navigationItem] setLeftBarButtonItem:leftItem];

    
    //定义导航栏右侧按钮
    UIButton *rightButton = [[UIButton alloc] initWithFrame:CGRectMake(0,0,54,27)];
    NSString *filePath2=[[NSBundle mainBundle] pathForResource:@"navRightBtn" ofType:@"png"];
    [rightButton setBackgroundImage:[UIImage imageWithContentsOfFile:filePath2] forState:UIControlStateNormal];
    [rightButton setBackgroundImage:[UIImage imageWithContentsOfFile:filePath2] forState:UIControlStateHighlighted];
    [rightButton setTitle:@"提 交" forState:UIControlStateNormal];
    rightButton.titleLabel.font=[UIFont fontWithName:@"Helvetica-Bold" size:14]; 
    [rightButton addTarget:self action:@selector(sendReport:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    //设置为左边按钮
    [[self navigationItem] setRightBarButtonItem:rightItem];

    
    
    
    UIImageView* bgView=[[UIImageView alloc] initWithFrame:CGRectMake(0,0,320,160)];
    NSString *filePath4=[[NSBundle mainBundle] pathForResource:@"text" ofType:@"png"];
    [bgView setImage:[UIImage imageWithContentsOfFile:filePath4]];  
    [bgView setUserInteractionEnabled:YES];  
    

    textViews = [[UITextView alloc] initWithFrame:CGRectMake(15, 10, 290, 140)];    
    //把换行键改成完成键
    textViews.returnKeyType = UIReturnKeyDone;
    textViews.textColor = [UIColor blackColor];
	textViews.font = [UIFont fontWithName:@"Arial" size:18.0];
    textViews.delegate = self;
	textViews.backgroundColor = [UIColor whiteColor];
//    self.textViews.returnKeyType = UIReturnKeyDefault;
	textViews.keyboardType = UIKeyboardTypeDefault;	
	textViews.scrollEnabled = YES;
	textViews.autoresizingMask = UIViewAutoresizingFlexibleHeight;
    [bgView addSubview:textViews];
    [self.view addSubview:bgView];


    
    //添加发送按钮
    UIButton *goBtn = [UIButton buttonWithType:UIButtonTypeCustom]; 
    [goBtn setFrame:CGRectMake(95, 170, 130, 40)];
    NSString *filePath5=[[NSBundle mainBundle] pathForResource:@"buttonBg" ofType:@"png"];
    [goBtn setBackgroundImage:[UIImage imageWithContentsOfFile:filePath5] forState:UIControlStateNormal];
    [goBtn setBackgroundImage:[UIImage imageWithContentsOfFile:filePath5] forState:UIControlStateHighlighted];
    [goBtn setTitle:@"提 交" forState:UIControlStateNormal];
    [goBtn addTarget:self action:@selector(sendReport:) forControlEvents:UIControlEventTouchUpInside];
    goBtn.titleLabel.font=[UIFont fontWithName:@"Helvetica-Bold" size:16];
    [self.view addSubview:goBtn];
    
    
}

//手势
-(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer{
    //后退
    if(recognizer.direction==UISwipeGestureRecognizerDirectionRight) {
        [self.navigationController popViewControllerAnimated:YES];
    }
}

//完成键调用的方法
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{

    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        return NO;
    }
    return YES;
} 


- (void)backButtonPressd{
    NSURL *system_sound_url=[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"1248" ofType:@"wav"]];
    SystemSoundID system_sound_id;
    AudioServicesCreateSystemSoundID((__bridge CFURLRef)system_sound_url, &system_sound_id);
    AudioServicesPlaySystemSound(system_sound_id);
    //将当前视图推出栈，程序将进入上一视图
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)sendReport:(id)sender{
    NSURL *system_sound_url=[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"1248" ofType:@"wav"]];
    SystemSoundID system_sound_id;
    AudioServicesCreateSystemSoundID((__bridge CFURLRef)system_sound_url, &system_sound_id);
    AudioServicesPlaySystemSound(system_sound_id);
    
    [textViews resignFirstResponder];
    
  
        

    
    MoreManager *manager = [[MoreManager alloc] init];
    NSString *type = [recieveDictionary objectForKey:@"type"];
    BOOL isSuccess;
    if ([type isEqualToString:@"F"]) {
        isSuccess = [manager sendFeedBack:textViews.text];
    }else{
        isSuccess = [manager sendErrorReport:textViews.text]; 
    }
    if (isSuccess) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"感谢您的及时反馈，点送的进步离不开您的支持" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
        [alertView show];
        return;
    }
    
}

- (void)viewDidUnload
{
    self.textViews=nil;
    [super viewDidUnload];
}

//- (void)dealloc {
//
//}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
